$(function () {
    $('#dg').datagrid({
        url: '/role/list',
        fit: true,
        fitColumns: true,
        //分页条
        pagination: true,
        // fit:true,
        //工具栏
        toolbar: "#tb",
        columns: [/*[
         {field: 'baseInfo', title: '基本属性', colspan: 2, width: 100, align: "center"},
         {field: 'otherInfo', title: '其他属性', colspan: 3, width: 100, align: "center"}
         ],*/ [
            {field: 'x', checkbox: 'true'},
            {field: 'id', title: '编码', width: 100},
            {field: 'name', title: '角色名称', width: 100},
            {field: 'sn', title: '角色编号', width: 100}
        ]],
        //新增
        border:false,
        cls:"table table-very table-basic"
    })


    var allData = {}; //缓存系统权限数据
    var menuData = {}; //缓存系统菜单数据


    $('#allPermissions').datagrid({
        url: '/permission/selectAll',
        title: "权限名称",
        width: 250,
        height: 350,
        //fit: true,
        //fitColumns: true,
        rownumbers: true,
        //单选
        singleSelect:true,
        columns: [[
            {field: 'id', title: '编码', width: 10, hidden: true},
            {field: 'name', title: '权限名', width: 200, align: 'center'}
        ]],
        onClickRow: function (index, row) {
            //双击删除一个
            $('#allPermissions').datagrid('deleteRow', index);

            //目标添加一个
            $('#selfPermissions').datagrid('appendRow', row);
        },
        onLoadSuccess: function (data) {
            allData = $.extend(true, {}, data);
            console.log(allData);
        }
    })

    //分配权限
    $('#selfPermissions').datagrid({
        title: "权限名称",
        width: 250,
        height: 350,
       // fit: true,
        //fitColumns: true,
        rownumbers: true,
        columns: [[
            {field: 'id', title: '编码', width: 10, hidden: true},
            {field: 'name', title: '权限名', width: 200, align: 'center'}
        ]],
        onClickRow: function (index, row) {
            //双击删除一个
            $('#selfPermissions').datagrid('deleteRow', index);

            //目标添加一个
            $('#allPermissions').datagrid('appendRow', row);
        },
        onLoadSuccess: function (data) {
            // allData = $.extend(true,{},data);
            var ids = $.map(data.rows, function (item) {
                return item.id;
            })
            //查询系统权限集合
            var rows = $('#allPermissions').datagrid('getRows');
            for (var i = rows.length - 1; i >= 0; i--) {
                if ($.inArray(rows[i].id, ids) >= 0) {
                    $('#allPermissions').datagrid('deleteRow', i);
                }
            }
        }

    })



    //================================================
    //分配菜单
    $('#allMenus').datagrid({
        url: '/systemMenu/selectAll',
        title: "菜单名称",
        width: 250,
        height: 350,
        //fit: true,
        //fitColumns: true,
        rownumbers: true,
        //单选
        singleSelect:true,
        columns: [[
            {field: 'id', title: '编码', width: 10, hidden: true},
            {field: 'name', title: '菜单名', width: 200, align: 'center'}
        ]],
        onClickRow: function (index, row) {
            //双击删除一个
            $('#allMenus').datagrid('deleteRow', index);

            //目标添加一个
            $('#selfMenus').datagrid('appendRow', row);
        },
        onLoadSuccess: function (data) {
            menuData = $.extend(true, {}, data);
            console.log(menuData);
        }
    })

    $('#selfMenus').datagrid({
        title: "菜单名称",
        width: 250,
        height: 350,
       // fit: true,
        //fitColumns: true,
        rownumbers: true,
        columns: [[
            {field: 'id', title: '编号', width: 10, hidden: true},
            {field: 'name', title: '菜单名', width: 200, align: 'center'}
        ]],
        onClickRow: function (index, row) {
            //双击删除一个
            $('#selfMenus').datagrid('deleteRow', index);

            //目标添加一个
            $('#allMenus').datagrid('appendRow', row);
        },
        onLoadSuccess: function (data) {
            var ids = $.map(data.rows, function (item) {
                return item.id;
            })
            //查询系统权限集合
            var rows = $('#allMenus').datagrid('getRows');
            for (var i = rows.length - 1; i >= 0; i--) {
                if ($.inArray(rows[i].id, ids) >= 0) {
                    $('#allMenus').datagrid('deleteRow', i);
                }
            }
        }

    })
    //=========分配菜单结束

//准备输入的对话框
    $('#dd').dialog({
        title: 'My Dialog',
        width: 600,
        height: 700,
        closed: true,
        // cache: false,
        modal: true,
        buttons: "#bb"

    })

    //将工具栏所有的函数集合在一个对象中
    var cmdObj;
    cmdObj = {
        //刷新emp页面
        reload: function () {
            $('#dg').datagrid("load")
        },
        //添加
        add: function () {
            //清除以前的数据
            $('#ff').form("clear");
            //清除自身权限
            $('#selfPermissions').datagrid('loadData', {total: 0, rows: []});
            $('#selfMenus').datagrid('loadData', {total: 0, rows: []});
            //加载缓存
            $('#allPermissions').datagrid('loadData', allData);
            $('#allMenus').datagrid('loadData', menuData);

            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "角色添加")
        },
        remove: function () {
            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }

            $.get("/role/delete", {id: row.id}, function (data) {
                if (data.succeed) {
                    $.messager.alert("温馨提示", "删除成功");
                    $('#dg').datagrid("reload")
                } else {
                    $.messager.alert("温馨提示", "删除失败");
                }
            })
        },
        edit: function () {

            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            console.log(row);
            $('#ff').form("clear");
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "角色编辑")
            //处理角色回显
            if (row.dept) {
                row["dept.id"] = row.dept.id;

            }
            //清除以前的数据
            $('#ff').form("clear");
            //回显
            $('#ff').form('load', row);
            $('#allPermissions').datagrid('loadData', allData);
            $('#allMenus').datagrid('loadData', menuData);
            //加载当前编辑角色的权限
            var options = $('#selfPermissions').datagrid('options');
            options.url = "/permission/queryPermissionByRoleId?roleId=" + row.id;

            //加载当前编辑角色的菜单
            var op = $('#selfMenus').datagrid('options');
            op.url = "/systemMenu/queryMenusByRoleId?roleId=" + row.id;

            //重新加载数据
            $('#selfPermissions').datagrid('load');
            //重新加载菜单
            $('#selfMenus').datagrid('load');


        },


        //高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue")
            var endDate = $("#endDate").textbox("getValue")
            $('#dg').datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate
            });
        },
        //保存数据[按钮]
        save: function () {

            //获取id
            var id = $("#empId").val();
            var url = "/role/saveOrUpdate";
            //提交表单
            $('#ff').form('submit', {
                url: url,
                //表单提交前封装额外参数
                onSubmit: function (param) {
                    var rows = $('#selfPermissions').datagrid('getRows');
                    for (var i = 0; i < rows.length; i++) {
                        param["permissions[" + i + "].id"] = rows[i].id;
                    }
                    var mrow = $('#selfMenus').datagrid('getRows');
                    for (var i = 0; i < mrow.length; i++) {
                        param["menus[" + i + "].id"] = mrow[i].id;
                    }
                },
                success: function (data) {
                    //把字符串转换为json
                    var data = $.parseJSON(data);
                    if (!data.succeed) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        $.messager.alert('温馨提示', "保存成功");
                        //关闭对话框
                        $('#dd').dialog("close");
                        //刷新datagrid
                        $('#dg').datagrid("reload")
                    }
                }
            });
        },
        //关闭按钮
        cancel: function () {
            $('#dd').dialog("close")
        }

    };

    //调用函数集合对象
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})




